package com.xbongbong.paas.service.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.service.FeeCompanyService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.companyorder.pojo.CompanyOrderDetailPojo;
import com.xbongbong.pro.companyorder.pojo.dto.CompanyOrderListDTO;
import com.xbongbong.pro.companyorder.pojo.vo.CompanyOrderListVO;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.PackageTypeEnum;
import com.xbongbong.pro.enums.ValueAddedTypeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.FeePayTypeEnum;
import com.xbongbong.sys.domain.entity.FeeCompanyEntity;
import com.xbongbong.sys.domain.entity.FeeLogEntity;
import com.xbongbong.sys.model.FeeCompanyModel;
import com.xbongbong.sys.model.FeeLogModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service("feeCompanyService")
public class FeeCompanyServiceImpl implements FeeCompanyService {

    @Resource
    private FeeCompanyModel feeCompanyModel;
    @Resource
    private FeeLogModel feeLogModel;

    @Override
    public CompanyOrderListVO list(@Valid CompanyOrderListDTO companyOrderListDTO) throws XbbException {
        CompanyOrderListVO listVO = new CompanyOrderListVO();
        String corpid = companyOrderListDTO.getCorpid();
        List<CompanyOrderDetailPojo> orderList = new ArrayList<>();
        List<FeeCompanyEntity> feeCompanyList = feeCompanyModel.getListByCorpid(corpid, companyOrderListDTO.getFeeNameLike());
        feeCompanyList.forEach(item ->{
            CompanyOrderDetailPojo detailPojo = new CompanyOrderDetailPojo();
            BeanUtil.copyProperties(item, detailPojo);
            PackageTypeEnum packageTypeEnum = PackageTypeEnum.getByType(item.getFeeType());
            detailPojo.setName(packageTypeEnum.getName());
            detailPojo.setPayType(FeePayTypeEnum.getByCode(item.getPayType()).getName());
            if (item.getEndTime()< DateUtil.getInt()) {
                detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.BE_EXPIRED));
            } else if (item.getIsPay() == 1) {
                detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.PAID));
            } else if (item.getIsPay() == 0) {
                detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.BE_PAID));
            }
            detailPojo.setOrderUser(item.getOpUserName());
            detailPojo.setOrderTime(Long.valueOf(item.getAddTime()));
            detailPojo.setExpireTime(item.getEndTime());
            detailPojo.setRealPrice(item.getFee());
            orderList.add(detailPojo);
        });
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("feeTypeIn", Arrays.asList(PackageTypeEnum.SEARCH_CUSTOMER.getType(), PackageTypeEnum.BUSINESS_SEARCH.getType(), PackageTypeEnum.ESIGN.getType(), PackageTypeEnum.DISTRIBUTOR.getType(), ValueAddedTypeEnum.CONTRACT_COMPARE.getType()));
        param.put("del", BasicConstant.ZERO);
        List<FeeLogEntity> feeLogList = feeLogModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(feeLogList)) {
            feeLogList.forEach(item -> {
                if (Objects.nonNull(item.getBuyNum()) && item.getBuyNum() > 0) {
                    CompanyOrderDetailPojo detailPojo = new CompanyOrderDetailPojo();
                    BeanUtil.copyProperties(item, detailPojo);
                    detailPojo.setRealPrice(item.getFee());
                    PackageTypeEnum packageTypeEnum = PackageTypeEnum.getByType(item.getFeeType());
                    detailPojo.setName(packageTypeEnum.getName());
                    detailPojo.setPayType(FeePayTypeEnum.getByCode(item.getPayType()).getName());
                    if (Objects.nonNull(item.getEndTime()) && item.getEndTime()< DateUtil.getInt()) {
                        detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.BE_EXPIRED));
                    } else if (item.getIsPay() == 1) {
                        detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.PAID));
                    } else if (item.getIsPay() == 0) {
                        detailPojo.setOrderStatus(I18nMessageUtil.getMessage(I18nStringConstant.BE_PAID));
                    }
                    detailPojo.setOrderUser(item.getOpUserName());
                    detailPojo.setOrderTime(Long.valueOf(item.getAddTime()));
                    detailPojo.setExpireTime(item.getEndTime());
                    orderList.add(detailPojo);
                }
            });
        }
        listVO.setOrderList(orderList);
        return listVO;
    }
}
